/*
 * SystemCallLayer.cpp
 *
 *  Created on: 10/07/2009
 *      Author: banouz
 */

#include "SystemCallLayer.h"

SystemCallLayer::SystemCallLayer()
{

  _fileSys = new FileSystem();
    _openFDTable = new map<int,fd*>();
    _revers_i_nodes_table = new map <string,int>() ;
    _i_nodes_table = new map<int,string>();


    if (pthread_mutex_init(&_mutex_lockB, NULL) != 0)
	{
			perror("mutex lock initialization");
			exit(1);
	}

   for(int i = 0; i < 128 ; i++){
   _fdNumbers[i] = 0;
   }

   int root = _fileSys->get_root_i_node();

   string rootString = "root";
   char* apN = new char[rootString.length()];
   strcpy(apN,rootString.c_str());

   add_to_tables(root,apN);



    _revers_i_nodes_table->insert(make_pair("root" ,root));
    _i_nodes_table->insert(make_pair(root,"root"));

 //   print_tables();
}

SystemCallLayer::~SystemCallLayer()
{
  // TODO Auto-generated destructor stub
}


 int SystemCallLayer::MakeFile(char* file_name,char type,int flag_access_permissions)
 {




	 if (_fileSys->check_space() < 0){
		 return -1;
	 }
	 map<string,int>::iterator iter;
	 iter = _revers_i_nodes_table->find(file_name);
	 if(iter != _revers_i_nodes_table->end()){

		 perror("file already exists (SC) ");
		 return -1;

	 }

	 string file_nameS = get_path(file_name,1); //new file name
	 char* file_nameT = new char[file_nameS.length()]; // the new file name (only its name without padding)
	 strcpy(file_nameT,file_nameS.c_str());


   int fdId = 0;
   int INood = 0;
   int i = 0;

   for( i = 0; i < 128 ; i++){
     if (_fdNumbers[i] == 0){
       _fdNumbers[i] = 1;
       fdId = i ;

       break;
       }
     }

   if ( i == 128){
    perror("There is no enough room for a new file");
    return -1;
   }

   char* newFileName = namePading(file_nameT);

   if ((type == 'f') |(type == 's') ){
     INood = _fileSys->createFile(type);
     fd* newFd = new fd();
     if (pthread_mutex_init(&(newFd->m_WriteLock), NULL) != 0)
       { perror("Read-Write mutex initialization");
       exit(1);}

     newFd->offset = 0;
     newFd->fdID = fdId;
     newFd->fileName = newFileName;
     newFd->lastPermis = -1;
     newFd->permissions = flag_access_permissions;
     newFd->pointer = INood;
     _openFDTable->insert(make_pair(fdId,newFd));



     ///////////////////////////////////////////////
     string path =  get_path(file_name,0);   //parent path (absolute)



     map<string, int>::iterator iter2;
 	//add the i node as a son to the parent dir
 	iter2 = _revers_i_nodes_table->find(path);
 	 if(iter2 == _revers_i_nodes_table->end()){

 			 perror("illegal path name (SC) ");
 			 return -1;

 	}


 	int i_node = iter2->second; //parents i node

 	vector<dir*>* vec = new vector<dir*>();


 	vec	= _fileSys->d_read(i_node);

 	dir* newDir = new dir();

 	newDir->_file_name = newFileName;

 	newDir->_iNode = INood;
 	vec->push_back(newDir);


 	_fileSys->d_write(i_node, vec);


    //add the absolute path to the tables
    _i_nodes_table->insert(make_pair(INood,file_name));
    _revers_i_nodes_table->insert(make_pair(file_name,INood));


     return fdId;
   }

   else{
     perror("Unknown file type : not a File type");
   }

   return -1;

 }

 int SystemCallLayer::MakeHLink(char* target_file_name, char* file_name)
 {
   int fdId = 0;
   int i = 0;

   for( i = 0; i < 128 ; i++){
     if (_fdNumbers[i] == 0){
    	 _fdNumbers[i] = 1;
       fdId = i ;
       break;
       }
     }

   if ( i == 128){
    perror("There is no enough room for a new file");
    return -1;
   }

   char* newTarFileName = namePading(target_file_name);

   string tStri = newTarFileName;
   string compTotri = "";
   map<int,fd*>::iterator iter = _openFDTable->begin();
   fd* tFd = new fd();
   fd* newFd = new fd();
   if (pthread_mutex_init(&(newFd->m_WriteLock), NULL) != 0)
     { perror("Read-Write mutex initialisation");
     exit(1);}

   while( iter != _openFDTable->end() ) {
     tFd = iter->second;
     compTotri = tFd->fileName;
     if(compTotri.compare(tStri) == 0){
       newFd->fdID = fdId;
       newFd->fileName = newTarFileName;
       newFd->offset = 0;
       newFd->lastPermis = tFd->lastPermis;
       newFd->permissions = tFd->permissions;
       newFd->pointer = tFd->pointer;   //Point to the same i-node
       _openFDTable->insert(make_pair(fdId,newFd));


       map<string, int>::iterator iter2;
       //add the i node as a son to the parent dir
       iter2 = _revers_i_nodes_table->find(target_file_name);
       int i_node = iter2->second;
       int num = _fileSys->get_hardLinks_num(i_node);
       _fileSys->set_hardLinks_num(i_node,num+1);

       return 0;
       //break;
     }
     iter++;
    }

   perror("Unknown file name : Hard Link");
   return -1;
 }

 int SystemCallLayer::MakeDir(char* dir_name)
 {

////////////////////////////////////////////////
	 if (_fileSys->check_space() < 0){
		perror("disk is full");
		 return -1;
	}

	 map<string,int>::iterator iter;
	 iter = _revers_i_nodes_table->find(dir_name);
	 if(iter != _revers_i_nodes_table->end()){

		 perror("dir already exists (SC) ");
		 return -1;

	 }



	 string path =  get_path(dir_name,0);   //parent path (absolute)  root
	 string dir_nameS = get_path(dir_name,1); //new dir name       ab



	 char* dir_nameT = new char[dir_nameS.length()]; // the new dir name (only its name without padding)
	 strcpy(dir_nameT,dir_nameS.c_str());

	 char* path_parent = new char[path.length()];  //parent path
	 strcpy(path_parent,path.c_str());


/////////////////////////////////////////////////////



	 int fdId = 0;
      int INood = 0;
      int i = 0;

      for( i = 0; i < 128 ; i++){
        if (_fdNumbers[i] == 0){
        	_fdNumbers[i] = 1;
          fdId = i ;
          break;
          }
        }

      if ( i == 128){
       perror("There is no enough room for a new file");
       return -1;
      }




      INood = _fileSys->createDir();


      fd* newFd = new fd();
      if (pthread_mutex_init(&(newFd->m_WriteLock), NULL) != 0)
        { perror("Read-Write mutex initialization");
        exit(1);}
      newFd->lastPermis = -1;
      newFd->offset = 0;
      newFd->fileName =namePading(dir_nameT);  //with padding
      newFd->permissions = RW; //TODO check for permissions
      newFd->pointer = INood;
      _openFDTable->insert(make_pair(fdId,newFd));




      ////////////////////////////////////////////////////////

     map<string, int>::iterator iter2;

	//add the i node as a son to the parent dir
	iter2 = _revers_i_nodes_table->find(path_parent);

	int i_node = iter2->second; //parents i node

	vector<dir*>* vec = new vector<dir*>();
	vec	= _fileSys->d_read(i_node);



	/*int co = 0 ;
	while(co < vec->size()){
			cout<<vec->at(co)->_file_name<<endl;
			co++;

	}*/

	dir* newDir = new dir();

	newDir->_file_name = namePading(dir_nameT);

	newDir->_iNode = INood;
	vec->push_back(newDir);

	int check = _fileSys->d_write(i_node, vec);
	if (check < 0){
		return -1;
	}

   //add the absolute path to the tables
   _i_nodes_table->insert(make_pair(INood,dir_name));
   _revers_i_nodes_table->insert(make_pair(dir_name,INood));


    return INood;

 }


 int SystemCallLayer::Open(char* filename, char flag_access_permissions)
 {
   string sFilename = filename;
   int fdNum = -1;
   map<string, int>::iterator iter = _revers_i_nodes_table->find(sFilename);//check if file exists

   if (iter != _revers_i_nodes_table->end()){


	   int i_node = iter->second;
	   fdNum = get_fd_by_iNode(i_node);
		if(_fileSys->getFileType(i_node) == 'f'){   //regular file
			cout<<"fd is :"<< fdNum  <<endl;
		   map<int, fd*>::iterator iter2 = _closeFDTable->find(fdNum);

		   if (iter2 != _closeFDTable->end())
			 {

			   return fdNum;
			 }
		   else
			 {
			   _openFDTable->insert(make_pair(iter2->first, iter2->second)); //Add to close table
			   _closeFDTable->erase(iter2);

			 }
		}else if(_fileSys->getFileType(i_node) == 's'){ //soft link
			cout<<"5??"<<endl;
			  int size = get_file_size(filename);
			  char* link = new char[size];
			  _fileSys->f_read(i_node,link,0,size);
			  if(is_exists(link)){
				  fdNum = Open(link,flag_access_permissions);
				  return fdNum;

			  }else{
				  return -1;
			  }

		}
   }

   else
    {
       perror("No such file to open");
       return -1;
    }

   return fdNum;
 }

  //Eran
  int SystemCallLayer::Close(int fdNum)
  {
    map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
          if( iter != _openFDTable->end() ) {
            _closeFDTable->insert(make_pair(iter->first,iter->second)); //Add to close table
            _openFDTable->erase(iter);
          }
          else{
            perror("No such a file to close");
            return -1;
          }
     return 1;
  }


 int SystemCallLayer::Seek(int fdNum, int location)
 {
   map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
   if( iter != _openFDTable->end() ) {
     fd* tFd = iter->second;
     tFd->offset += location;
   }else{
     perror("No match for given fd : Seek");
   }

   return 0;
 }

 int SystemCallLayer::Read (int fdNum, int nBytes, char *Buffer)
 {
   map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
      if( iter != _openFDTable->end() ) {
        fd* tFd = iter->second;
        int i_node = tFd->pointer;
        int offset = tFd->offset;
        if((tFd->permissions == 0) | (tFd->permissions == 2)) { //TODO check default permission
          _fileSys->f_read(i_node,Buffer,offset,nBytes);
          tFd->offset += nBytes;
        }
        else{
          perror("Can not read to this file : no permissions");
          return -1;
        }
      }
      else{
        perror("No such file to read into");
        return -1;
      }
      return 1;
 }

 int SystemCallLayer::Write (int fdNum, int nBytes,char* Buffer)
 {
   map<int,fd*>::iterator iter = _openFDTable->find(fdNum);

   if( iter != _openFDTable->end() ) {
     fd* tFd = iter->second;
     int i_node = tFd->pointer;
     int offset = tFd->offset;
     if((tFd->permissions > 0) & (tFd->permissions < 3)) { //TODO check default permission
       _fileSys->f_write(i_node,Buffer,offset,nBytes);
       tFd->offset += nBytes;
     }
     else{
       perror("Can not write to this file : no permissions");
       return -1;
     }
   }
   else{
     perror("No such file to write into");
     return -1;
   }
   return 1;
 }


 int SystemCallLayer::lockRead (int fdNum)
 {
   map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
   if( iter != _openFDTable->end() ) {
     fd* tFd = iter->second;

     if ((tFd->permissions) != WO ) {
       pthread_mutex_lock(&(tFd->m_WriteLock));
         if (tFd->lastPermis == -1){          //This check for prevent wrong changes
           tFd->lastPermis = tFd->permissions;
           tFd->permissions = RO;
         }
         tFd->numOfRead = tFd->numOfRead + 1;
       pthread_mutex_unlock(&(tFd->m_WriteLock));
     }
     else{
       perror("Can not lock this file for read only : Writing is in progress");
       return -1;
     }
   }
   else{
     perror("Can not lock this file for read only : File dosen't exist");
     return -1;
   }
   return 1;
 }

 int SystemCallLayer::lockWrite (int fdNum)
 {
   map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
   if( iter != _openFDTable->end() ) {
     fd* tFd = iter->second;

     if ((tFd->permissions) != RO ) {
       pthread_mutex_lock(&(tFd->m_WriteLock));
         if (tFd->lastPermis == -1){     //This check : for prevent wrong changes
           tFd->lastPermis = tFd->permissions;
           tFd->permissions = WO;
         }
         else{
           perror("Can not lock this file for write only : lastPermis");
            return -1;
         }
       pthread_mutex_unlock(&(tFd->m_WriteLock));
     }
     else{
       perror("Can not lock this file for write only : Reading is in progress");
       return -1;
     }
   }
   else{
     perror("Can not lock this file for write only : File dosen't exist");
     return -1;
   }
   return 1;
 }

int SystemCallLayer::releaseLockRead (int fdNum)
{
  map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
  if( iter != _openFDTable->end() ) {
    fd* tFd = iter->second;

    if ((tFd->permissions) == RO ) {
      pthread_mutex_lock(&(tFd->m_WriteLock));
        if (tFd->numOfRead == 1){          //This check for prevent wrong changes
        tFd->permissions = tFd->lastPermis;
        tFd->numOfRead = tFd->numOfRead - 1;
        tFd->lastPermis = -1;
        }
        else{
          tFd->numOfRead = tFd->numOfRead - 1;
        }
      pthread_mutex_unlock(&(tFd->m_WriteLock));
    }
    else{
      perror("Can not release this file : Reading is in progress");
      return -1;
    }
  }
  else{
    perror("Can not release this file : File dosen't exist");
    return -1;
  }
  return 1;
}

int SystemCallLayer::releaseLockWrite(int fdNum)
{
  map<int,fd*>::iterator iter = _openFDTable->find(fdNum);
  if( iter != _openFDTable->end() ) {
    fd* tFd = iter->second;

    if ((tFd->permissions) == WO ) {
      pthread_mutex_lock(&(tFd->m_WriteLock));
        if (tFd->lastPermis == -1){          //This check for prevent wrong changes
        tFd->permissions = tFd->lastPermis;
        tFd->lastPermis = -1;
        }
      pthread_mutex_unlock(&(tFd->m_WriteLock));
    }
    else{
      perror("Can not release this file : Reading is in progress");
      return -1;
    }
  }
  else{
    perror("Can not release this file : File dosen't exist");
    return -1;
  }
  return 1;
}

/*
 * add ? to complete 12 Byte name
 */
char* SystemCallLayer::namePading(char* file_name) {

 bool cond = true;
  char* newFileName = new char[12];
        for(int i = 0;i < 12 ;i++) {
          if (file_name[i] != 0 & cond) {
            newFileName[i] = file_name[i];
          }else {
        	 cond = false;
            newFileName[i] = '?';
          }
        }

   return newFileName;
}
void SystemCallLayer::add_to_tables(int i_node,char* ap)
{

        dir* tDir;
        int iNode;
        char* name;
        vector<dir*>* dirs = _fileSys->d_read(i_node);


        for(unsigned int i = 0; i < dirs->size() ; i++){


                tDir = dirs->at(i);
                iNode = tDir->_iNode;
                name = tDir->_file_name;

                string str = (string)name;
                int pos = str.find('?',0);
                string n = str.substr(0,pos);
             //   char* nameTrc = new char[n.length()];
            //    strcpy(nameTrc,n.c_str());

                string s;
                s.append(ap);
                s.append("/");
                s.append(n);
                char* path = new char[s.length()];
                strcpy(path,s.c_str());

               if(str.compare(".") != 0){

				   _revers_i_nodes_table->insert(make_pair(s,iNode));
					_i_nodes_table->insert(make_pair(iNode,s));
                }
                if( (_fileSys->getFileType(iNode) == 'd') && (str.compare(".") != 0) ){
						add_to_tables(iNode,path);
                }

        }


}
////////////////////
int SystemCallLayer::RmDir(char* dir_name)
{

        map<string,int>::iterator iter;
        iter = _revers_i_nodes_table->find(dir_name);
        if(iter == _revers_i_nodes_table->end()){

                perror("cannot delete dir . dir  does not exists (SC) ");

                return -1;

        }

        int iNode =  iter->second;


        string path =  get_path(dir_name,0);  //parent absolute path

        map<string, int>::iterator iter2;

        //add the i node as a son to the parent dir
        iter2 = _revers_i_nodes_table->find(path);

        int i_node = iter2->second; //parents i node



        vector<dir*>* vec = new vector<dir*>();
        vec	= _fileSys->d_read(i_node);
        int pos = 0;

        for(unsigned int i = 0 ; i < vec->size() ; i++){

        	if ((vec->at(i)->_iNode) == iNode){
        		pos = i;
        		i = vec->size();
        	}

        }


        vec->erase(vec->begin() + pos);
        _fileSys->d_write(i_node,vec);


        _revers_i_nodes_table->erase(dir_name);
        _i_nodes_table->erase(iNode);

       int check = _fileSys->d_delete(iNode);
	   if (check != 0 ){
			   perror("cannot delete dir (SC)");

			   return -1;
	   }




        return 0;

}

int SystemCallLayer::RmFile(char* file_name)
 {
         map<string,int>::iterator iter;
		 iter = _revers_i_nodes_table->find(file_name);
		 if(iter == _revers_i_nodes_table->end()){

						 perror("cannot delete file . file  does not exists (SC) ");
						 return -1;

		 }

		 int iNode =  iter->second;  // i node found in revers i node table for file_name (ap)


		string path =  get_path(file_name,0);  //parent absolute path
		map<string, int>::iterator iter2;

		//add the i node as a son to the parent dir
		iter2 = _revers_i_nodes_table->find(path);
		int i_node = iter2->second; //parents i node

		vector<dir*>* vec = new vector<dir*>();
		vec	= _fileSys->d_read(i_node);
		int pos = 0;

		for(unsigned int i = 0 ; i < vec->size() ; i++){

			if ((vec->at(i)->_iNode) == iNode){
				pos = i;
				i = vec->size();
			}

		}


		vec->erase(vec->begin() + pos);
		_fileSys->d_write(i_node,vec);


		 _revers_i_nodes_table->erase(file_name);
		 _i_nodes_table->erase(iNode);


		 int check = _fileSys->f_delete(iNode);
		 if (check != 0 ){
						 perror("cannot delete file (SC)");
						 return -1;
		 }


		 return 0;
 }

bool SystemCallLayer::isDir(char *address)
{


	//	bool ans = false;
        map<string,int>::iterator iter;
        iter = _revers_i_nodes_table->find(address);
        if(iter == _revers_i_nodes_table->end()){
                return false;
        }

        int iNode =  iter->second;
        if ( (_fileSys->getFileType(iNode)) == 'd'){
               return true;
        }

        if ( (_fileSys->getFileType(iNode)) == 's'){

			  int size = get_file_size(address);
              char* link = new char[size];

              _fileSys->f_read(iNode,link,0,size);
             // cout<<"read : "<<link<<endl;
              if(is_exists(link)){
            	  map<string,int>::iterator iter2;
            	  iter2 = _revers_i_nodes_table->find(link);
            	  int i_node2 = iter2->second;
            	  if ( (_fileSys->getFileType(i_node2)) == 'd'){
            	        return true;
            	  }else{
            		  return false;
            	  }

              }else{
            	  return false;
              }

         }

        return false;

}

int SystemCallLayer::moveFile(char* oldName, char* newName)
{
		cout<<"old name : "<<oldName<<endl;
        string path_old =  get_path(oldName,0);
        string path_new =  get_path(newName,0);

        string file_old = get_path(oldName,1);
        string file_new =  get_path(newName,1);

        string n = "";

        char* file = new char[file_old.length()];
        strcpy(file,file_old.c_str());

        int Inode = 0;
        if (file_old.compare(file_new) == 0){
                return 0;
        }


        //rename file
        if (path_old.compare(path_new) == 0){

                map<string,int>::iterator iter;
                iter = _revers_i_nodes_table->find(path_old);
                if (iter == _revers_i_nodes_table->end()){
                        perror("old file name does not exists, rename file failed (FS)");
                        return -1;
                }

                Inode =  iter->second;
                vector<dir*>* old = _fileSys->d_read(Inode);

                for(unsigned int i = 0; i<old->size() ; i++){

                        dir* tDir = old->at(i);
                        char* file = tDir->_file_name;

                        string str = file;
                        int pos = str.find('?',0);
                        n = str.substr(0,pos);

                        if(n.compare(file_old) == 0){



								char* newF = new char[file_new.length()];
                                strcpy(newF,file_new.c_str());
                                (old->at(i))->_file_name = namePading(newF);




                                _revers_i_nodes_table->erase(oldName);
                                _revers_i_nodes_table->insert(make_pair(newName,Inode));
                              if(isDir(oldName)){

                            	  _fileSys->d_write(Inode,old);

                              }else{

                            	int fdID =  get_fd_by_iNode(Inode);
                            	  cout<<"1"<<endl;
                                map<int,fd*>::iterator fdIter;
                                cout<<"111"<<endl;
                                fdIter = _openFDTable->find(fdID);

                                // (fdIter->second)->fileName  =  newF;

                                char type = _fileSys->getFileType(Inode);
                                int flag_access_permissions = (fdIter->second)->permissions;
                                cout<<"2"<<endl;
                                RmFile(oldName);
                                cout<<"3"<<endl;
                                MakeFile(newName,type,flag_access_permissions);
                                cout<<"4"<<endl;
                              }

                                i = old->size();
                        }
                }






         }/// end of rename file
        else{

        	/// add to new dir/////////
        	  string directory = newName;
			 map<string, int>::iterator iter2;

			//add the i node as a son to the new dir
			iter2 = _revers_i_nodes_table->find(directory);
			int i_node = iter2->second; //new dir i node

			vector<dir*>* vec = new vector<dir*>();
			vec	= _fileSys->d_read(i_node);

			dir* newDir = new dir();
			newDir->_file_name = namePading(file);
			newDir->_iNode = Inode;

			vec->push_back(newDir);
			_fileSys->d_write(i_node, vec); //update new dir with new file inside.


			/////// remove from old dir //////////////
			 map<string, int>::iterator iter3;
			iter3 = _revers_i_nodes_table->find(path_old);
			int i_nodeD = iter3->second; //old dir i node

			vector<dir*>* vecD = new vector<dir*>();
			vecD	= _fileSys->d_read(i_nodeD);
			int pos = 0;

			for(unsigned int i = 0 ; i < vecD->size() ; i++){

				if ((vecD->at(i)->_iNode) == Inode){
					pos = i;
					i = vecD->size();
				}

			}

			vecD->erase(vecD->begin() + pos);
			_fileSys->d_write(i_nodeD,vecD);

        }


        return 0;

}

string SystemCallLayer::get_path(char* path,int type)
{

        string all = path;

        int pos = 0;
        int res = 0;
        unsigned int i = 0;

        while (i < all.length()){

                pos = all.find('/',i);
                if(pos != -1){
                        res = pos;
                }
                i++;

        }

        string ans;

        if(type == 0){
                ans = all.substr(0,res);   //path
        }else if(type == 1){
                ans = all.substr(res+1,all.length());    //filename
        }else{
        	 ans = all.substr(res,all.length());
        }
        return ans;

}


int SystemCallLayer::ls(char* dir_name, char* buf)
 {

           string ans;
           map<string,int>::iterator iter;
           iter = _revers_i_nodes_table->find(dir_name);
           if(iter == _revers_i_nodes_table->end()){

                        perror("no such dir... (SC) ");
                        return -1;

          }

           int iNode = iter->second;
           vector<dir*>*  list =  _fileSys->d_read(iNode);

           for(int i = 0; i < list->size() ; i++){

			dir* tDir = new dir();
			tDir =	list->at(i);
			char* name = tDir->_file_name;
			string str = name;
			int pos = str.find('?',0);
			string n = str.substr(0,pos);
			ans.append(n);
			ans.append("*");

           }
           strcpy(buf,ans.c_str());
         //  cout<<"buffer in sc : "<<buf<<endl;

           return 0;

 }

void SystemCallLayer::print_tables()
{

	cout<< " reverse nodes table "<<endl;
	map<string,int>::iterator iter;
	iter = _revers_i_nodes_table->begin();
	while(iter != _revers_i_nodes_table->end()){

		cout<<iter->first<<"  "<<iter->second<<endl;
		iter++;

	}


	cout<< " I nodes table "<<endl;

	map<int,string>::iterator iter2;
	iter2 = _i_nodes_table->begin();
	while(iter2 != _i_nodes_table->end()){

		cout<<iter2->first<<"  "<<iter2->second<<endl;
		iter2++;

	}

	cout<<"---------------------------------------"<<endl;



}

int SystemCallLayer::get_file_size(char* file_name)
{

	   map<string,int>::iterator iterS;
	  //print_tables();
	   iterS = _revers_i_nodes_table->find(file_name);
	   if(iterS == _revers_i_nodes_table->end()){

			perror("no such file... (SC) ");
			return -1;

	  }


	   int i_node =  iterS->second;

	   int size = _fileSys->get_file_size(i_node);
	   return size;

}

bool SystemCallLayer::is_exists(char *file_name)
{

	  map<string,int>::iterator iter;
	   iter = _revers_i_nodes_table->find(file_name);
	   if(iter == _revers_i_nodes_table->end()){
		   return false;
	   }

	   return true;


}

bool SystemCallLayer::is_fd_open(int fdID)
{
	map<int,fd*>::iterator iter;

	iter = _openFDTable->find(fdID);

	if (iter == _openFDTable->end()){
		return false;
	}

	return true;

}

int SystemCallLayer::get_fd_by_iNode(int i_node)
{

	map<int,fd*>::iterator iter = _openFDTable->begin();
	while(iter != _openFDTable->end()){

		fd* f_d = new fd();
		f_d = iter->second;
		cout<<"fd is:" << f_d->pointer<<endl;
		if (f_d->pointer == i_node ){
			return iter->first;
		}
		iter++;
	}




	return -1;
}



















